package com.imachen.basic.service.impl;

import com.imachen.basic.mapper.BaseMapper;
import com.imachen.basic.query.BaseQuery;
import com.imachen.basic.service.IBaseService;
import com.imachen.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> BaseMapper;

    @Override
    public void add(T t) {
        BaseMapper.add(t);
    }

    @Override
    public void delete(Long id) {
        BaseMapper.delete(id);
    }

    @Override
    public void update(T t) {
        BaseMapper.update(t);
    }

    @Override
    public T findOneById(Long id) {
        return BaseMapper.findOneById(id);
    }

    @Override
    public List<T> findAll() {
        return BaseMapper.findAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        Integer totals = BaseMapper.queryCount(baseQuery);
        if(totals == 0){
            return new PageList<>();
        }
        List<T> data = BaseMapper.queryData(baseQuery);
        return new PageList<>(data,totals);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        BaseMapper.patchDelete(ids);
    }
}
